﻿
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace GSOrganization.Application;

public class LogModels : BaseService
{

    public static void WriteLog(SysLogInput entity)
    {
        try
        {

            //此处实现日志的记录
            var item = entity.Adapt<SysLog>();

            item.Id = Result.GetNewId();
            item.CreatedTime = DateTime.Now;

            BaseService baseService = new BaseService();
            if (!string.IsNullOrWhiteSpace(baseService.UserId) && !string.IsNullOrWhiteSpace(baseService.UserName))
            {
                item.PersonId = baseService.UserId;//唯一标识
                item.Name = baseService.UserName;
            }


            string localIp = NetworkInterface.GetAllNetworkInterfaces()
                 .Select(p => p.GetIPProperties())
                 .SelectMany(p => p.UnicastAddresses)
                 .FirstOrDefault(p => p.Address.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(p.Address))?.Address.ToString();
            item.Ip = localIp;

            var _syslog = Db.GetRepository<SysLog>();
            _syslog.Insert(item);
            _syslog.SaveNow();

        }
        catch (Exception ep)
        {
            try
            {
                var webRootPath = App.HostEnvironment.ContentRootPath;
                // string path = @"mylog.txt";
                string txtPath = webRootPath;//
                using (StreamWriter sw = new StreamWriter(txtPath, true, Encoding.Default))
                {
                    sw.WriteLine((ep.Message + "||" + DateTime.Now.ToString()).ToString());
                    sw.Close();
                }

            }
            catch
            {
            }
        }

    }
    public static void LoginWriteLog(SysLogInput entity)
    {
        try
        {

            //此处实现日志的记录
            var item = entity.Adapt<SysLog>();

            item.Id = Result.GetNewId();
            item.CreatedTime = DateTime.Now;

            BaseService baseService = new BaseService();
            item.PersonId = entity.PersonId;//唯一标识
            item.Name = entity.Name;

            string localIp = NetworkInterface.GetAllNetworkInterfaces()
                 .Select(p => p.GetIPProperties())
                 .SelectMany(p => p.UnicastAddresses)
                 .FirstOrDefault(p => p.Address.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(p.Address))?.Address.ToString();
            item.Ip = localIp;

            var _syslog = Db.GetRepository<SysLog>();
            _syslog.Insert(item);
            _syslog.SaveNow();

        }
        catch (Exception ep)
        {
            try
            {
                var webRootPath = App.HostEnvironment.ContentRootPath;
                // string path = @"mylog.txt";
                string txtPath = webRootPath;//
                using (StreamWriter sw = new StreamWriter(txtPath, true, Encoding.Default))
                {
                    sw.WriteLine((ep.Message + "||" + DateTime.Now.ToString()).ToString());
                    sw.Close();
                }

            }
            catch
            {
            }
        }

    }

    public void Dispose()
    {

    }
}
